package com.liusy.taskengine.executemodel;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.liusy.taskengine.resultmodel.ComparResult;
import com.liusy.taskengine.taskexecute.AbstractTaskExecute;
import com.liusy.taskengine.taskexecute.ITaskExecute;
import com.liusy.taskengine.taskmodel.ITaskModel;

/**
 * 抽像的执行者接口
 * 
 *  @author liusy 2012-12-11
 */
public abstract class AbstractExecuteModel implements  IExecuteModel{
	
	private static Logger log = Logger.getLogger(AbstractExecuteModel.class);

	protected String executeModelName = "我是执行任务使者。。。。。。。";

	//任务模型
	protected ITaskModel taskModel=null;
	
	//运行结果
	protected int exeRsult=0;
	
	//执行行者的工具
	protected ITaskExecute taskExecute = null;
	
	
	
	public AbstractExecuteModel()
	{
		
	}
	
	public AbstractExecuteModel(String executeModelName)
	{
		this.executeModelName = executeModelName;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.liusy.taskengine.executemodel.IExecuteModel#RunExecute(com.liusy.taskengine.taskmodel.ITaskModel, com.liusy.taskengine.taskexecute.ITaskExecute)
	 */
	@Override
	public void RunExecute(ITaskModel taskModel,ITaskExecute taskExecute) {
		this.taskModel = taskModel;
		this.taskExecute = taskExecute;
		try {
			
			RunBefore(taskModel);
			
			List<Map<String,Object>>  list = RunTask(taskModel);
			
			RunAfter(taskModel,list);
			
			if(null!=list)
			{
				//log.info(taskModel.getSql());
				//new ComparResult(taskModel.getRowMap(), list, taskModel.getSql()).start(((AbstractTaskExecute)this.taskExecute).getTaskManager().getResultModel());
				((AbstractTaskExecute)this.taskExecute).getTaskManager().addComparResults(new ComparResult(taskModel.getRowMap(), list, taskModel.getSql()));
				this.setTaskExecuteComplete(true);
			}
			
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
	}
	


	@Override
	public void RunBefore(ITaskModel taskModel) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void RunAfter(ITaskModel taskModel,List<Map<String,Object>> list) {
	
		
	}

	public void setTaskExecuteComplete(boolean flag) {
		if(flag)
		{
			this.taskModel = null;
			((AbstractTaskExecute)this.taskExecute).taskModel=null;
		}
	}
	
	
	public boolean IsTaskExecuteComplete() {
		if(null==taskModel)
			return true;
		return false;
	}
	
	
	public ITaskExecute getTaskExecute() {
		return taskExecute;
	}


	public void setTaskExecute(ITaskExecute taskExecute) {
		this.taskExecute = taskExecute;
	}


	public ITaskModel getTaskModel() {
		return taskModel;
	}

	public void setTaskModel(ITaskModel taskModel) {
		this.taskModel = taskModel;
	}

	public int getExeRsult() {
		return exeRsult;
	}

	public void setExeRsult(int exeRsult) {
		this.exeRsult = exeRsult;
	}


	public String getExecuteModelName() {
		return executeModelName;
	}


	public void setExecuteModelName(String executeModelName) {
		this.executeModelName = executeModelName;
	}
	
	
}
